package com.dev.common.utils;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.dev.common.constant.ConfigKeyConstants;

/**
 * @Classname SendMailUtils
 * @Description 发送邮箱
 * @Date 2019/9/29 11:25
 * @Created by SSL
 */
@Component
public class SendMailUtils
{
    private static String SMTP_HOST     = "host";

    private static String SMTP_PORT     = "port";

    private static String MAIL_USER     = "user";

    private static String MAIL_PASSWORD = "pwd";

    private static String TITLE         = "title";

    private static String CONTENT       = "content";

    public static boolean sendForgotPwd(String toMail, String code)
    {
        JSONObject configData = JSONObject.parseObject(RedisUtils.get(ConfigKeyConstants.FORGOT_MAIL).toString());
        JSONObject data = configData.getJSONObject("configValue");
        String title = data.getString(TITLE);
        String content = data.getString(CONTENT).replace("TO_MAIL", toMail).replace("FORGOT_CODE", code);
        // 格式化
        return sendGoogleMail(toMail, title, content, data);
    }

    // 发送邮件
    @SuppressWarnings("restriction")
    public static boolean sendGoogleMail(String toMail, String title, String content, JSONObject configData)
    {
        // 配置ssl
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // 配置发送邮件的环境属性
        Properties props = System.getProperties();
        props.put("mail.debug", "true");
        props.put("mail.smtp.host", configData.getString(SMTP_HOST));
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.port", configData.getString(SMTP_PORT));
        props.put("mail.smtp.socketFactory.port", configData.getString(SMTP_PORT));
        props.put("mail.smtp.auth", "true");
        String user = configData.getString(MAIL_USER);
        String pwd = configData.getString(MAIL_PASSWORD);
        props.put("mail.user", user);
        props.put("mail.password", pwd);
        // 构建授权信息，用于进行SMTP进行身份验证
        Session session = Session.getDefaultInstance(props, new Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                // 用户名、密码
                return new PasswordAuthentication(user, pwd);
            }
        });
        try
        {
            // 创建邮件消息
            Message msg = new MimeMessage(session);
            // 设置发件人邮件地址和名称。填写控制台配置的发信地址,比如xxx@xxx.com。和上面的mail.user保持一致。名称用户可以自定义填写。
            msg.setFrom(new InternetAddress(configData.getString(MAIL_USER)));
            // 设置收件人邮件地址，比如yyy@yyy.com
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail, false));
            // 设置邮件标题
            msg.setSubject(title);
            // 设置邮件的内容体
            msg.setText(content);
            msg.setSentDate(new Date());
            Transport.send(msg);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
